package com.chang.multithread;

import lombok.extern.slf4j.Slf4j;
//abc 按顺序交替输出5次
@Slf4j
/**
 * 输出内容 标记 下一标记
 * a        1        2
 * b        2        3
 * c        3        1
 */
public class abcWaitNotify {
    private int loopNumber;
    private int flag;

    public abcWaitNotify(int loopNumber, int flag) {
        this.loopNumber = loopNumber;
        this.flag = flag;
    }

    //打印
    public void print(String str,int waitFlag,int nextFlag){
        for (int i = 0; i < loopNumber; i++) {
            synchronized (this){
                while (flag!=waitFlag){
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace(); }
                }
                    System.out.print(str);
                    flag=nextFlag;
                    this.notifyAll();
            }
        }

    }

    public static void main(String[] args) {
        abcWaitNotify wn=new abcWaitNotify(5, 1);
        new Thread(()->{
            wn.print("a", 1, 2);
        }).start();
        new Thread(()->{
            wn.print("b", 2, 3);
        }).start();
        new Thread(()->{
            wn.print("c", 3, 1);
        }).start();
    }
}
